#pragma once
#include <iostream>
#include <vector>
#include <functional>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

namespace My_Process_Pool
{
    using work_in = std::function<void(int t)>;

    work_in login_work      = [](int t) -> void{sleep(t); std::cout<<"login   ..."<<std::endl; };
    work_in logout_work     = [](int t) -> void{sleep(t); std::cout<<"logout  ..."<<std::endl; };
    work_in send_work       = [](int t) -> void{sleep(t); std::cout<<"send    ..."<<std::endl; };
    work_in receive_work    = [](int t) -> void{sleep(t); std::cout<<"receive ..."<<std::endl; };
    work_in save_work       = [](int t) -> void{sleep(t); std::cout<<"save    ..."<<std::endl; };
    work_in find_work       = [](int t) -> void{sleep(t); std::cout<<"find    ..."<<std::endl; };

    // 任务类
    class Task_Pool
    {
    public:
        // 默认构造
        Task_Pool()
        {
            _works.emplace_back(login_work);
            _works.emplace_back(logout_work);
            _works.emplace_back(send_work);
            _works.emplace_back(receive_work);
            _works.emplace_back(save_work);
            _works.emplace_back(find_work);
            //_size = 6;
        }
        // 拷贝构造
        Task_Pool(const Task_Pool &cpy) = delete;
        // 赋值重载
        Task_Pool operator=(const Task_Pool &cpy) = delete;
        // 析构
        ~Task_Pool() = default;

        // 工作任务
        void _workrun(int index, int t)
        {
            if (index > 0 && index < _works.size())
            {
                _works[index](t);
            }
        }

        // 等待
        int _wait(int fd)
        {
            ssize_t tasknum = 0;
            int buffer = 0;
            while (tasknum == 0)
            {
                tasknum = ::read(fd, &buffer, sizeof(buffer));
                if (tasknum != 0)
                {
                    std::cout << "_wait num -> " << buffer << std::endl;
                    return buffer;
                }
            }
            return 0;
        }

    private:
        std::vector<work_in> _works;
        //int _size = 0;
    };
}
